package com.itheima.health.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.health.common.MessageConst;
import com.itheima.health.config.QiniuConfiguration;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.User;
import com.itheima.health.pojo.Users;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 验证码控制层
 *
 * @author liuqi
 * @Date 2023/11/22
 */
@Slf4j
@RestController
@RequestMapping("/mobile")
public class MobileController {
    /**
     * 注入redis模版对象
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private SetMealService setMealService;

    @Autowired
    QiniuConfiguration qiniuConfiguration;


    private static final String SIGN = "阿里云短信测试";
    private static final String TEMPLATE_CODE = "SMS_154950909";

    /**
     * 预约验证码校验
     */
    @PostMapping("/validateCode/send")
    public Result send(String type, String telephone) {
        log.info("[体检预约-预约验证码校验]type：{}，telephone：{}", type, telephone);
        // 生成四位数随机数
        String param = RandomUtil.randomNumbers(4);
        // 调用工具类发送短信
//        SMSUtils.sendMessage(SIGN, TEMPLATE_CODE, telephone, param);
        String key = type + "_" + telephone;
        log.info("{},code is {}",key,param);
        // 将验证码存储到redis中
        redisTemplate.opsForValue().set(key, param, 2, TimeUnit.MINUTES);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

    @PostMapping("/login/smsLogin")
    public Result login(@RequestBody Users user ){
        log.info("====>{}", user);
        User users=  userService.login(user);
        return new Result(true, MessageConst.ACTION_SUCCESS,users);
    }

    /**
     * 查询所有套餐
     * @return
     */
    @GetMapping("/setmeal/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> setmeals=this.setMealService.getSetmeal();
        return new Result(true,MessageConst.ACTION_SUCCESS,setmeals);
    }

    /**
     * 套餐详情查看
     */
    @GetMapping("/setmeal/findById")
    public Result findById(String id) {
        log.info("[根据id查询套餐详情：{}]", id);
        Setmeal setmeal = setMealService.findById(id);
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, setmeal);
    }
}

